unit udlgRenameCatalog;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Rtti, System.Classes,
  System.Variants, FMX.Types, FMX.Controls, FMX.Forms, FMX.Dialogs, ufrmCustomDlg,
  uProjectDic, FMX.Objects, FMX.Edit, FMX.Layouts;

type

  TdlgRenameCatalog = class(TfrmCustomDlg)
    lNameDesc: TLabel;
    eName: TEdit;
    procedure eNameChange(Sender: TObject);
    procedure eNameKeyDown(Sender: TObject; var Key: Word; var KeyChar: Char;
      Shift: TShiftState);
    procedure btOkClick(Sender: TObject);
  protected
    FProjectDic: TProjectDic;
    function CheckInput(AShowAlert: boolean): boolean; override;
    procedure DoOkClick(); virtual;
  public
    procedure PrepareView(AProjectDic: TProjectDic); virtual;

    property ProjectDic: TProjectDic read FProjectDic;
  end;

  function dlgRenameCatalog(AOwner: TFmxObject; AProjectDic: TProjectDic): boolean;


implementation

uses uStringConst;

{$R *.fmx}


function dlgRenameCatalog(AOwner: TFmxObject; AProjectDic: TProjectDic): boolean;
var
  dlgRenameCatalog: TdlgRenameCatalog;
begin
  dlgRenameCatalog:= TdlgRenameCatalog.Create(AOwner);
  dlgRenameCatalog.PrepareView(AProjectDic);
  dlgRenameCatalog.ShowModal;
  result:= dlgRenameCatalog.Ok;
  dlgRenameCatalog.Free;
end;


procedure TdlgRenameCatalog.btOkClick(Sender: TObject);
begin
  DoOkClick();
end;

function TdlgRenameCatalog.CheckInput(AShowAlert: boolean): boolean;
var
  i: TProjectDic;
begin
  if Length(eName.Text) = 0 then begin
    if AShowAlert then
      ShowAlert( gst_dlgRenameCatalog_NoNameH, gst_dlgRenameCatalog_NoName);
    exit( False );
  end;

  i:= dicProjectCatalogs.Find( eName.Text );
  if (i <> nil) and (((FProjectDic <> nil) and (i <> FProjectDic)) or (FProjectDic = nil))  then begin
    if AShowAlert then
      ShowAlert(gst_dlgRenameCatalog_NameDublH, gst_dlgRenameCatalog_NameDubl);
    exit( False );
  end;

  exit( True )
end;

procedure TdlgRenameCatalog.DoOkClick;
begin
  if not CheckInput( True ) then begin
    eName.SetFocus;
    exit;
  end;

  FProjectDic.CatalogName:= eName.Text;
  FProjectDic.dbSave();

  Fok:= True;
  Close;
end;

procedure TdlgRenameCatalog.eNameChange(Sender: TObject);
begin
  if CheckInput(True) then
    ClearAlert();
end;

procedure TdlgRenameCatalog.eNameKeyDown(Sender: TObject; var Key: Word; var KeyChar: Char;
  Shift: TShiftState);
begin
  if Key = vkReturn then
    btOkClick(Sender);
end;

procedure TdlgRenameCatalog.PrepareView(AProjectDic: TProjectDic);
begin
  if AProjectDic <> nil then begin
    FProjectDic:= AProjectDic;
    eName.Text:= FProjectDic.CatalogName;
    eName.SelectAll;
  end;
end;

end.
